package tmi.li.txwidgets.txwthr;


import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build.VERSION;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;


import tmi.li.txwidgets.TxWidgetsFinals;
import tmi.li.txwidgets.R;





public class TxWthrProvider extends AppWidgetProvider {
	private static String TAG = "tmi.li.txwidgets.txwthr.TxWthrProvider";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		for(int id : appWidgetIds) {
			Log.d(TAG, "onUpdate() id => "+id);
			TxWthrWidget widget = new TxWthrWidget(context);
			if(widget.load(id) == true && (widget._woeid != null || widget._woeid != "")) {
				Intent intent = new Intent(TxWidgetsFinals.TXWTHR_BUILD_UPDATE);
				context.sendBroadcast(intent);  
			}
		}
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {

		// When the user deletes the widget, delete the preference associated with it.
		TxWthrWidget widget = new TxWthrWidget(context);
		for(int id : appWidgetIds) {
			if(widget.load(id)) {
				widget.deleteWidget();
			}
		}
	}

	@Override
	public void onEnabled(Context context) {
		Log.d(TAG, "onEnabled()");
		context.startService(new Intent(context, Update.class));
	}

	@Override
	public void onDisabled(Context context) {
		Log.d(TAG, "onDisabled()");
		context.stopService(new Intent(context, Update.class));

	}


	static public void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {



		TxWthrWidget widget = new TxWthrWidget(context);

		if(true == widget.load(appWidgetId) && (widget._current != null || widget._current != "")) {
			//getting the Widget views
			RemoteViews views = null;   
			views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
			//if the sdk version is >= 8 aka android 2.2 froyo set the Background color
			if(VERSION.SDK_INT >= 8) {
				views.setInt(R.id.lWidget, "setBackgroundColor", Color.parseColor("#"+widget._bcolor));
			}

			//setting the current temperature and description
			views.setTextViewText(R.id.Left, widget._current);
			views.setTextViewText(R.id.Top, widget._desc);
			views.setTextViewText(R.id.Bot, widget._desc2);

			//setting the Font Color for the Widget
			views.setTextColor(R.id.Left, Color.parseColor("#"+widget._flcolor));
			if(widget._ftcolor != null)
				views.setTextColor(R.id.Top, Color.parseColor("#"+widget._ftcolor));
			if(widget._fbcolor != null)
				views.setTextColor(R.id.Bot, Color.parseColor("#"+widget._fbcolor));

			try {
				//setting the OnClick Pending INtent for the whole Layout
				String appname;
				if((appname = widget._appname) != null && widget._appname != "null" && widget._appname != "" && (widget._appname.equals("update") == false)) {
					Log.d(TAG, "updateWidget() appname => "+widget._appname+" app");
					Intent intent = new Intent(Intent.ACTION_MAIN);
					intent.setComponent(ComponentName.unflattenFromString(appname));
					PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
					views.setOnClickPendingIntent(R.id.lWidget, pendingIntent);
				}else if((appname = widget._appname) == "update" || widget._appname.equals("update")) {
					Log.d(TAG, "updateWidget() appname => "+widget._appname+" update");
					PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(TxWidgetsFinals.TXWTHR_BUILD_UPDATE), PendingIntent.FLAG_UPDATE_CURRENT);
					views.setOnClickPendingIntent(R.id.lWidget, pendingIntent);
				}
			}catch(Exception e) {

			}
			//updating the widget via the widget manager
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}



	public static class Update extends Service {
		private static final String TAGSERVICE = "tmi.li.txwidgets.txwthr$Update";
		private TxWthrReceiver mReceiver;
		
		@Override
		public void onCreate() {
			Log.d(TAGSERVICE, "onCreate()");
			mReceiver = new TxWthrReceiver();
			this.registerReceiver(mReceiver, new IntentFilter(TxWidgetsFinals.TXWTHR_UPDATE));	
			this.registerReceiver(mReceiver,  new IntentFilter(TxWidgetsFinals.TXWTHR_BUILD_UPDATE));
		}
		

		@Override
	    public int onStartCommand(Intent intent, int flags, int startId) 
	    {
	    	Log.d(TAGSERVICE, "onStartCommand()");	        
 
	        // We want this service to continue running until it is explicitly
	        // stopped, so return sticky.
	        return START_STICKY;
	    }
		
		@Override
		public void onDestroy() {
			// TODO Auto-generated method stub
			this.unregisterReceiver(mReceiver);
			super.onDestroy();
		}

		@Override
		public IBinder onBind(Intent intent) {
			return null;
		}


	}
}